<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?
require_once  '../classes/ConnectionDB.php';
require_once  '../classes/UtenteManager.php';
require_once  '../classes/TestDataManager.php';
require_once  '../classes/DisciplinaDataManager.php';
require_once  '../bean/Tests.php';

$connection =ConnectionDB::getInstance()->connectToDB();
$codiceUtente = $_COOKIE['codiceUtente'];
$result = UtenteManager::getInstance()->getUtente($connection, $codiceUtente);

$argomenti =TestDataManager::getInstance()->getArgomenti($connection, $codiceUtente);

$disciplineUtente = DisciplinaDataManager::getInstance()->getDisciplineUtente($connection);
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Report test</title>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/prototype.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/glassbox/glassbox.js" type="text/javascript"></script>
        <style type="text/css">
            @import url("../css/style.css");
            table{
                width: 100%;
            }

            #tableResults td{
                text-align: center;
            }

            #divUpdate
            {
                display: none;
            }


            #searchForm {
                padding-top: 3%;
            }

            #searchFormCont
            {
                padding-left: 16%;
                padding-top: 6%;
            }

           

        </style>
        <script type="text/javascript">
            path_to_root_dir = "../glassboxLatest/glassbox.2.0.0/";
            var listaDomande;
            var numItem=8;
            var currentIndex=0;

            window.onload = function()
            {
                var searchForm = new GlassBox();
                searchForm.init( 'searchForm', '660px', '200px', 'hidden','',true, false);
                searchForm.ipos();
                searchForm.appear();

                var results = new GlassBox();
                results.init( 'results', '1140px', '390px', '','',true,false);
                results.ipos();
                results.appear();
            }

            function xXMLHttpRequest()
            {

                var client = null;
                if (window.XMLHttpRequest)
                {
                    client=new XMLHttpRequest();
                }
                // code for IE
                else if (window.ActiveXObject)
                {
                    client=new ActiveXObject("Microsoft.XMLHTTP");
                }

                return client;
            }

            function handler()
            {
                if(this.readyState == 4 && this.status == 200)
                {
                    //          alert("resp : " + this.responseText);
                    var resp = eval(this.responseText);
                    listaDomande = new Array();
                    if(resp!=null)
                    {
                        for(i=0;i<resp.length; i++)
                        {
                            listaDomande[i] = resp[i];
                        }
                    }
                    manageNavig(0);


                } else if (this.readyState == 4 && this.status != 200) {
                    // fetched the wrong page or network error...
                    alert("error this.status : " + this.status );

                }
            }

            function handlerUpdate() {
                if(this.readyState == 4 && this.status == 200)
                {
                    document.getElementById('divUpdate').style.display="block";
                    find();
                } else if (this.readyState == 4 && this.status != 200) {
                    // fetched the wrong page or network error...
                    alert("error this.status : " + this.status );

                }
            }


            function find()
            {
                var client = xXMLHttpRequest();
                client.onreadystatechange = handler;
                var nome = document.getElementById('nome').value;
                var codiceDisciplina = document.getElementById('codiceDisciplina').value;

                var param = "nome="+ nome+"&codiceDisciplina="+codiceDisciplina;
                client.open("POST", "reportTestListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);

            }



            function updateOperation(ind)
            {
                var codiceTest = document.getElementById('codiceTest'+ind).value;
                var nomeTest = document.getElementById('nomeTest'+ind).value;
                var oreTest = document.getElementById('oreTest'+ind).value;
                var minutiTest = document.getElementById('minutiTest'+ind).value;

                var strErr="";

                if(nomeTest.length==0)
                    strErr+="16*";
                if(oreTest.length==0)
                    strErr+="17*";
                if(parseInt(oreTest)<0)
                    strErr+="18*";
                if(minutiTest.length==0)
                    strErr+="19*";
                if(parseInt(minutiTest)<0 || parseInt(minutiTest) >60)
                    strErr+="20*";

                if(strErr.length>0)
                {
                    location.href='../portal/manageErrors.php?lista='+strErr;
                }
                else
                {

                    var client = xXMLHttpRequest();
                    client.onreadystatechange = handlerUpdate;

                    var param = "operation=update"
                        +"&codiceTest="+codiceTest
                        +"&nomeTest="+nomeTest
                        +"&oreTest="+oreTest
                        +"&minutiTest="+minutiTest;

                    client.open("POST", "reportTestListener.php", true);
                    client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                    client.send(param);
                }
            }

            function sottomettiTest(codiceTest)
            {
                location.href='../testManaging/liveTest.php?codiceTest='+codiceTest;
            }
            function eliminaOperation(ind)
            {
                var codiceTest = document.getElementById('codiceTest'+ind).value;
                var client = xXMLHttpRequest();
                client.onreadystatechange = handlerUpdate;

                var param = "operation=delete"
                    +"&codiceTest="+codiceTest;

                client.open("POST", "reportTestListener.php", true);
                client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

                client.send(param);
            }


            function manageNavig(ind)
            {
                var tmp= new Array();

                var str="<table border='1' id='tableResults' cellpadding='5'>"
                    +"<tr>"
                    +"<th>Codice Test</th>"
                    +"<th>Data creazione</th>"
                    +"<th>Nome</th>"
                    +"<th>Disciplina</th>"
                    +"<th>Tempo</th>"
                    +"<th>Elenco Domande</th>"
                    +"<th>Modifica</th>"
                    +"<th>Elimina</th>"
                    +"<th>Sottometti test</th>"
                    +"</tr>";
                var indCurrenct=0;
                while(indCurrenct<numItem)
                {
                    //                    alert("idn " + ind);
                    if(ind<listaDomande.length)
                    {
                        tmp=listaDomande[ind];
                        var nome=tmp.nome;


                        str+='<input type="hidden" id="codiceTest'+tmp.codiceTest+'" value="'+tmp.codiceTest +'"/><tr><td>'+tmp.codiceTest+'</td>'+
                            '<td>'+tmp.dataCreazione+'</td>'+
                            '<td><input type="text" id="nomeTest'+tmp.codiceTest+'" name="nome" value="'+nome+'"/></td>'+
                            '<td>'+tmp.nomeDisciplina+'</td>'+
                            '<td><input type="text" id="oreTest'+tmp.codiceTest+'" name="ore" value="'+tmp.ore +'" size="2" /> hh <input type="text" name="minuti" id="minutiTest'+tmp.codiceTest+'" value="' + tmp.minuti +'" size="2" /> mm</td>'+
                            '<td><img src="../images/icons/PNG/My Documents.png" height="30" width="30" title="Elenco domande" border="0" onclick="javascript:location.href=\'testQuestions.php?codiceTest='+tmp.codiceTest+'\'"/>'+
                            '<td><input type="button" value="Modifica" onClick="javascript:updateOperation('+tmp.codiceTest+')" /></td>'+
                            '<td><input type="button" value="Elimina" onClick="javascript:eliminaOperation('+tmp.codiceTest+')" /></td>'+
                            '<td><input type="button" value="Sottometti test" onClick="javascript:sottomettiTest('+tmp.codiceTest+')" /></td>'+
                            '</tr>';

                    }
                    ind++;
                    indCurrenct++;
                }
                str +="</table>";
                var numTotItem = listaDomande.length;
                var numPag = Math.ceil(numTotItem / numItem);
                if(numPag > 1)
                {
                    str +="<div id='navigator'><center>";
                    var current = 0;
                    var toPrint=1
                    for(k=1;k<=numPag;k++)
                    {
                        str+="<a href='javascript:manageNavig("+current+")'>"+toPrint+"</a>&nbsp;&nbsp;";
                        current= k * numItem;
                        toPrint = toPrint+1;
                    }
                    str+="</center></div>";
                }
                document.getElementById('resultsCont').innerHTML=str;
            }

            function showDetail(codice)
            {
                location.href="detailQuestion.php?codiceDomanda=" + codice;
            }
        </script>
    </head>
    <body>
        <?php include '../portal/header.php';?>
        <center>

            <div id="searchForm">
                <div id="searchFormCont">
                    <center>
                        <table>

                            <tr>
                                <td>Nome:</td>
                                <td><input type="text" id="nome" name="nome" value="" /></td>
                            </tr>

                            <tr>
                                <td>Disciplina:</td>
                                <td>
                                    <select name="codiceDisciplina" id="codiceDisciplina">
                                        <?
                                        for($i=0;$i<count($disciplineUtente); $i++)
                                        {

                                            $disciplinaDesc = DisciplinaDataManager::getInstance()->getDisciplina($connection, $disciplineUtente[$i]);

                                            $str = trim($disciplinaDesc->getNome()); ?>
                                        <option value="<?= $disciplinaDesc->getCodice()?>"><?= $str ?></option>
                                            <?
                                        }?>
                                    </select>
                                </td>
                            </tr>


                        </table>
                    </center>
                    <br />
                    <input type="button" value="Cerca" onclick="find();" /><div id="divUpdate" style="color: green">Test modificato con successo.</div>

                </div>


            </div>
            <div id="results"><div id="resultsCont"></div></div>
        </center>
    </body>
</html>
